<?php

namespace app\admin\model;

use think\Model;


class Need extends Model
{

    

    

    // 表名
    protected $name = 'need';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'b_time_text',

        'read_status_text',
        'time_text',
        'd_time_text'
    ];
    

    


    public function getReadStatusList()
    {
        return ['0' => __('Read_status 0'), '1' => __('Read_status 1'), '2' => __('Read_status 2')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5')];
    }

    public function getBTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['b_time']) ? $data['b_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getReadStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['read_status']) ? $data['read_status'] : '');
        $list = $this->getReadStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['time']) ? $data['time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['d_time']) ? $data['d_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setBTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function clients()
    {
        return $this->belongsTo('Clients', 'clients_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
